package sk.stuba.fiit.clientApp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.ListDataListener;

public class InitializationUtil {
	private static Properties items;
	private static Properties certificateProperties;
	
	static{
		items = new Properties();
		certificateProperties = new Properties();
		try {
//			items.load(new  FileInputStream(new File("./src/main/resources/app.confing")));
			items.load(new  FileInputStream(new File("app.confing")));
			certificateProperties.load(new FileInputStream(new File("./CA.properties")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static ComboBoxModel getHashMethod() {	
		String algorithm = items.getProperty("hash_algorithm");
		String[] algorithms = algorithm.split(",");
		DefaultComboBoxModel model = new DefaultComboBoxModel(algorithms);
		return model;
	}
	
	public static ComboBoxModel getSignatureMethod(){
		String algorithm = items.getProperty("digest_algorithm");
		String[] algorithms = algorithm.split(",");
		DefaultComboBoxModel model = new DefaultComboBoxModel(algorithms);
		return model;
	}
	
	public static ComboBoxModel getSignaturePolicy(){
		String algorithm = items.getProperty("signature_plicy");
		String[] algorithms = algorithm.split(",");
		DefaultComboBoxModel model = new DefaultComboBoxModel(algorithms);
		return model;
	}
	
	public static ComboBoxModel getArchiveOperation(){
		String operation = items.getProperty("archive_operation");
		String[] operations = operation.split(",");
		DefaultComboBoxModel model = new DefaultComboBoxModel(operations);
		return model;
	}
	
	public static ComboBoxModel getPrefOperation(){
		String preferencie = items.getProperty("preference_operation");
		String[] preferencies = preferencie.split(",");
		DefaultComboBoxModel model = new DefaultComboBoxModel(preferencies);
		return model;
	}

	public static Properties getCertificateProperties() {
		return certificateProperties;
	}
		
	
	public static void setUserDetail(String name,String password, String id) throws FileNotFoundException, IOException{
		items.setProperty("user_name", name);
		items.setProperty("user_password", password);
		items.setProperty("user_id", id);
		items.store(new FileOutputStream(new File("./src/main/resources/app.confing")), "");
	}
	
	public static String getUserName(){
		return items.getProperty("user_name");
	}
	
	public static String getUserPassword(){
		return items.getProperty("user_password");
	}
	
	public static String getUserId(){
		return items.getProperty("user_id");
	}
	

}
